package cc.geguo.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

import org.apache.struts2.interceptor.RequestAware;

import java.util.List;
import java.util.Map;

import javax.xml.ws.RequestWrapper;

import cc.geguo.entity.Admin;
import cc.geguo.service.AdminService;

/**
 * 作者  Administrator
 * 时间  2017/2/17 13:29
 * 文件  StrutsLoginDemo
 * 描述  这个是控制层
 * 1、封装请求数据
 *
 * 2、调用Service
 *
 */
public class UserAction extends ActionSupport implements RequestAware{
	/**==================================*/
	private Admin admin;

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}


	/**----------------------------------*/

	private AdminService adminService = new AdminService();
	/**
	 * @return 登录
	 */
	public String login() {
		Admin userInfo = null;
		try {
			userInfo = adminService.login(getAdmin());
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		if (userInfo==null){
//			登录失败
			System.out.println("登陆失败");
			return INPUT;
		}else {
//			保存到Session中
			ActionContext.getContext().getSession().put("userInfo",userInfo);
			System.out.println("登陆成功");
			return "loginSuccess";
		}
	}

	/**
	 * @return 列表
	 */
	public String list() {
		try {
			List<Admin> list = adminService.getAll();
			if (list==null){
				System.out.println("数据库查询失败");
			}

//			保存到Request中
//			ActionContext.getContext().getContextMap().put("listAdmin",list);
//			保存到Session域中
			ActionContext.getContext().getSession().put("listAdmin",list);
			return "list";
		} catch (Exception e) {
			return ERROR;
		}
	}

	/**
	 * @return 数据回显技术
	 * 将要跳转页面的 表单需要的数据 传递过去(通过栈顶)
	 */
	public String viewupdate(){
		Admin admin = new Admin();
		admin.setUserName("传递过去的");
		admin.setPwd("12345");

		ValueStack vs = ActionContext.getContext().getValueStack();
		vs.push(admin);
		return "update";

	}

	private Map<String,Object> request;
	@Override
	public void setRequest(Map<String, Object> map) {
		this.request = map;
	}
}
